package com.jd.sunny.java.collection;

import java.util.*;

/**
 * @Description:
 * @author:sunlin
 * @since:2012-12-07 10:47
 * @version:1.0.0
 */
public class TestCollections {

    public static void main(String[] args){

        //return a unchangeable List
        List<String> list = Collections.nCopies(100,"default");

        //singleton
        String name = "sunny";
        Set set =  Collections.singleton(name);
        System.out.println(set);

        /**
         * sort
         * if we use sort(List<T> list) ,the object must implements Comparable
         * else if we use sort(List<T> list, Comparator<? super T> c) ,we need new a Comparator
         */
        List<String> staff = new LinkedList<String>();
        staff.add("Sunny");
        staff.add("Edition");
        staff.add("Obama");
        Collections.sort(staff);
        System.out.println("sort" + staff);
        /**
         * we use reverse() to reverse order
         */
        Collections.reverse(staff);
        System.out.println("reverse" + staff);

        /**
         * reverse the default Comparator
         * we can also reverse specified Comparator
         */
        Comparator comparator = Collections.reverseOrder();
        Comparator c =   Collections.reverseOrder(comparator);

        /**
         * binary search : 二分法查找,LinkedList was converted to linear search,list must be sort first
         */
        List<String> worker = new ArrayList<String>();
        worker.add("Sunny");
        worker.add("Edition");
        worker.add("Obama");

        // worker must be sort first
        Collections.sort(worker);
        System.out.println("worker=" + worker);
        Collections.reverseOrder();
        System.out.println("reverse worker=" + worker);

        int index =  Collections.binarySearch(worker,"Sunny");
        System.out.println("binary search index=" + index);
        /**
         * shuffle do not ensure the result
         */
        Collections.shuffle(staff);
        System.out.println("shuffle" + staff);


    }
}
